package com.tongji.seller.model
{
	import com.adobe.serialization.json.JSON;
	import com.tongji.seller.events.UpdateItemImageEvent;
	import com.tongji.seller.vo.ObjectTranslator;
	import com.tongji.seller.vo.Product;
	
	import mx.collections.ArrayCollection;
	
	import org.robotlegs.mvcs.Actor;
	
	/**
	 * 
	 * @author jiarong
	 * 2011-12-6
	 *
	 */
	
	public class StoreProductModel extends Actor
	{

		public var savedProductArray:ArrayCollection = new ArrayCollection();
		
		[Inject]
		public var userModel:UserModel;
		
		public var arrarIndex:int;//表示object在list中的位置，但是不知道为什么是从8开始的
		public var nowIndex:int = 0;//累加用的变量，会进行模3运算，用于切换三张图片
		public var product:String;
		public var productArr:ArrayCollection;
		[Bindable]
		public var storeProductArray:ArrayCollection;
		public var allProductArray:ArrayCollection;
		public var productCount:Number;
		public var savedProductCount:Number; //用户选择过的宝贝
		[Bindable]
		public var savedProductCountStr:String;
		
		public var selectedProductArray:ArrayCollection = new ArrayCollection();
		public var currentProduct:Object;
		public var imageList:ArrayCollection;
		
		public var uploadedProductArray:ArrayCollection = new ArrayCollection();//存放所上传的宝贝图片
		
		
		public var leftPageIndex:int = 1
		public var rightPageIndex:int = 1;
		

		public var tempSelectedPro:ArrayCollection = new ArrayCollection();//临时存放选择的宝贝
		public var deletePro:Object;
		public var imageState:int;//0表示增加图片，1表示修改图片 
		
		[Bindable]
		public var storeProduct:ArrayCollection;
		
		public function StoreProductModel()
		{
			super();
		}
		/**
		 *保存的宝贝 
		 * @param product
		 *
		 * 
		 */		
		public function updateStoreItem(product:String):void
		{
			this.product = product;
			var object:Object = (JSON.decode(product) as Object);
			savedProductCount = object.PageCount;
			savedProductCountStr = String(savedProductCount);
			if (savedProductCount != 0)
			{
				var arr:Array = [];
				arr = object.ProductList;
				storeProductArray = new ArrayCollection(arr);
				//var storeProductArray:ArrayCollection = new ArrayCollection(arr);
				/*if (arr.length % 10 == 0)
				{
					storeProductArray = new ArrayCollection(arr);
				}
				else
				{
					var nullCount:int = 10 - arr.length % 10;
					var tempArray:Array = new Array();
					tempArray.length = nullCount;
					
					storeProductArray = new ArrayCollection(arr.concat(tempArray));
				}*/
				
				if (allProductArray != null)
				     resetProductState(storeProductArray);
				/*for each(var obj:Object in arr)
				{
					//var pro:Product = ObjectTranslator.objectToInstance(obj, Product);
					storeProductArray.addItem(obj);
				}*/
			}
			/*else
			{
				storeProductArray = new ArrayCollection();
			}*/
			//var arr:Array = (JSON.decode(product) as Array);
			//productArr = new ArrayCollection(arr);
			//storeProductArray = new ArrayCollection(productArr[0].VR_SceneList);
		}
		
		/**
		 * 用于第三步中的宝贝数据 
		 * @param product
		 * 
		 */		
		public function updateStoreProduct(product:String):void
		{
			this.product = product;
			var object:Object = (JSON.decode(product) as Object);
			/*savedProductCount = object.PageCount;
			savedProductCountStr = String(savedProductCount);*/
			if (savedProductCount != 0)
			{
				var arr:Array = [];
				arr = object.ProductList;
				storeProduct = new ArrayCollection(arr);
				
				/*for each(var obj:Object in arr)
				{
				//var pro:Product = ObjectTranslator.objectToInstance(obj, Product);
				storeProductArray.addItem(obj);
				}*/
			}
			/*else
			{
			storeProductArray = new ArrayCollection();
			}*/
			//var arr:Array = (JSON.decode(product) as Array);
			//productArr = new ArrayCollection(arr);
			//storeProductArray = new ArrayCollection(productArr[0].VR_SceneList);
		}
		/**
		 *店铺所有宝贝 
		 * @param product
		 * @return 
		 * 
		 */		
		
		public function updateAllItem(product:String):void
		{
			this.product = product;
			var object:Object = (JSON.decode(product) as Object);
			productCount = object.items_get_response.total_results;
			if (productCount != 0)
			{
				var arr:Array = [];
				arr = object.items_get_response.items.item;
				var tempArray:ArrayCollection = new ArrayCollection(arr);
				/*for each(var obj:Object in arr)
				{
					var pro:Product = ObjectTranslator.objectToInstance(obj, Product);
					tempArray.addItem(pro);
				}*/
				allProductArray = tempArray;
				if(savedProductArray != null)
					resetProductState(savedProductArray);
				
				if(tempSelectedPro.length > 0)
					resetProductState(tempSelectedPro);
			}
			else
			{
				allProductArray = new ArrayCollection();
			}
		}
		
		public function updateSavedProduct(product:String):void
		{
			this.product = product;
			var object:Object = (JSON.decode(product) as Object);
			savedProductArray = new ArrayCollection(object.ProductList);
			if (allProductArray != null)
				resetProductState(savedProductArray);
		}
		
		/**
		 * 源数据与已保存的数据进行对比，重新设置每个场景的选择信息
		 * @param sceneArray
		 * @return ArrayCollection
		 * 
		 */		
		private function resetProductState(savedProductArray:ArrayCollection):void
		{
			var sceneCount:int = allProductArray.length;
			for each(var object:Object in savedProductArray)
			{
				for (var i:int = 0; i < sceneCount; i++)
				{
					if (allProductArray.getItemAt(i).pic_url == object.pImgUrl)
					{
						allProductArray.getItemAt(i).selected = true;
					}
				}
			}
			allProductArray.refresh();
		}
		
		public function checkStoreProduct():Boolean
		{
			for each(var object:Object in storeProductArray)
			{
				if (object.ItemImgList == null)
					return false;
				/*var obj:Object = object.ItemImgList;
				if(obj.imgOne == null && obj.imgTwo == null && obj.imgThree == null)*/
					
			}
			return true;
		}
		
		public function getImageByProduct(product:Object):void
		{
			imageList = new ArrayCollection();
			var str:String = "assets/images/image.png";
			
			if (product.ItemImgList != null)
			{
				var imgOne:Object = new Object();
				imgOne.id = 1;
				imgOne.edited = 0;
				imgOne.newurl = null;
				if (product.ItemImgList.imgOne != null)
				{
					imgOne.url = product.ItemImgList.imgOne;
					imgOne.edited = 1;
				}
				else
					imgOne.url = str;
				
				imageList.addItem(imgOne);
				
				var imgTwo:Object = new Object();
				imgTwo.id = 2;
				imgTwo.edited = 0;
				imgTwo.newurl = null;
				if (product.ItemImgList.imgTwo != null)
				{
					imgTwo.url = product.ItemImgList.imgTwo;
					imgTwo.edited = 1;
				}
					
				else
					imgTwo.url = str;
				
				imageList.addItem(imgTwo);
				
				var imgThree:Object = new Object();
				imgThree.id = 3;
				imgThree.edited = 0;
				imgThree.newurl = null;
				if (product.ItemImgList.imgThree != null)
				{
					imgThree.url = product.ItemImgList.imgThree;
					imgThree.edited = 1;
				}
				else
					imgThree.url = str;
				
				imageList.addItem(imgThree);
				 
				if(imgOne.edited == 1 || imgTwo.edited == 1 || imgThree.edited == 1)
					imageState = 1;
			}
			else
			{
				var imgOne1:Object = new Object();
				imgOne1.id = 1;
				imgOne1.edited = 0;
				imgOne1.newurl = null;
				imgOne1.url = str;
				
				imageList.addItem(imgOne1);
				
				var imgTwo1:Object = new Object();
				imgTwo1.id = 2;
				imgTwo1.edited = 0;
				imgTwo1.newurl = null;
				imgTwo1.url = str;
				
				imageList.addItem(imgTwo1);
				
				var imgThree1:Object = new Object();
				imgThree1.id = 3;
				imgThree1.edited = 0;
				imgThree1.newurl = null;
				imgThree1.url = str;
				
				imageList.addItem(imgThree1);
				
				imageState = 0;//表示增加图片
			}

		}
		public function updatePngImg(imgs:ArrayCollection):void
		{
			var imges:Array = new Array();
			for each(var obj:Object in imageList)
			{
				if (obj.edited == 1 && obj.newurl == null)
				{
					var str:String = obj.url;
					var strarr:Array = str.split("http://img.rryjw.com");
					imges.push(strarr[1]);
				}
			}
			for each(var img:Object in imgs)
			{
				imges.push(img);
			}
			dispatch(new UpdateItemImageEvent(UpdateItemImageEvent.UPDATE_IMAGE, currentProduct.ItemImgList.iId, imges));
		}
		public function removeProduct():void
		{
			var sceneCount:int = allProductArray.length;
			for (var i:int = 0; i < sceneCount; i++)
			{
				if (allProductArray.getItemAt(i).num_iid == deletePro.detailUrl)
				{
					allProductArray.getItemAt(i).selected = false;
				}
			}
		}
	}
}